
package ling.archives.controller;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.*;
import ling.archives.domain.dto.ArchiveContentXls;
import ling.archives.domain.dto.ArchiveXls;
import ling.archives.domain.dto.FilingApprovalRequest;
import ling.archives.service.ArchiveService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.*;
import lombok.*;
import com.ruoyi.common.core.page.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import ling.archives.domain.Archive;
//import ling.archives.service.ArchiveService;
import ling.archives.common.*;
import ling.archives.common.domain.*;
import org.springframework.web.multipart.MultipartFile;

/** 卷宗 控制层
 * @author : He-LiangLiang@Outlook.com
 * @date : 2025-3-26
 */
@Api(tags = "卷宗 功能接口")
@RestController
@RequestMapping("/archives/Archive")
public class ArchiveController extends EQueryController<Archive>{


    @Resource
    ArchiveService archiveService;

    @ApiOperation("详情")
    @PreAuthorize("@ar.any(@ar.archive.query)")
    @GetMapping(value = "/{id}")
    public Result<Archive> getInfo(@PathVariable("id") Long id)
    {
        return super.getInfo(id);
    }
    @ApiOperation("分页列表")
    @PreAuthorize("@ar.any(@ar.archive.list)")
    @GetMapping("list")
    public ResultRows<Archive> list(Archive dto, PageSort pageRequest) {
        var re = queryPage(dto, pageRequest);
        var ds = archiveService.DoccTypeList();
        re.getData().forEach(p ->
                ds.stream().filter(x -> Objects.equals(x.getId(), p.getDoccId()))
                        .findFirst()
                        .ifPresent(z -> p.ExtendMapSet("doccId_title", z.getTitle()))
        );
        return re;
    }

    @ApiOperation("新增")
    @PreAuthorize("@ar.any(@ar.archive.add)")
    @Log(title = "卷宗", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public Result add(@Validated @RequestBody Archive entity){
        return super.add(entity);
    }

    @ApiOperation("删除")
    @PreAuthorize("@ar.any(@ar.archive.remove)")
    @Log(title = "卷宗", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable("ids") Long[] ids){
        return super.remove(Arrays.stream(ids).toArray());
    }

    @ApiOperation("编辑")
    @PreAuthorize("@ar.any(@ar.archive.edit)")
    @Log(title = "卷宗", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public  Result edit(@Validated @RequestBody Archive entity){
        return super.edit(entity);
    }

    //todo 归档需要审批
    @ApiOperation("归档申请")
    @PreAuthorize("@ar.any(@ar.archive.fileng)")
    @PostMapping("filing")
    public Result<List<String>> filing(@RequestBody Long[] ids, String remark){

        return  archiveService.filing(ids,remark);
    }

    @ApiOperation("归档审批")
    @PreAuthorize("@ar.any(@ar.archive.fileng)")
    @PostMapping("filingApproval")
    Result<String> filingApproval(@RequestBody @Valid FilingApprovalRequest request){
        return archiveService.filingApproval(request.getApprovalId(), request.getIsPass(), request.getRemark());
    }
    @ApiOperation("导出")
    @PreAuthorize("@ar.any(@ar.archive.export)")
    @Log(title = "卷宗", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(Archive entity, PageDomain pageRequest,HttpServletResponse response)
    {
        super.export(entity,pageRequest,null,response);
    }

    @ApiOperation("导入")
    @Log(title = "卷宗", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ar.any(@ar.archive.importData)")
    @PostMapping("/importData")
    public Result<Collection<String>> importData(MultipartFile file, Long documentClassId) throws Exception {
        if(documentClassId==null||documentClassId<1)
            return Result.bad("分类不能为空");
        var util = new ExcelUtilEx<>(ArchiveXls.class);
        var archiveXlsList = util.importExcel("卷皮", file.getInputStream(), 0);
        var contentList = util.newByWorkbook(ArchiveContentXls.class).importExcel("卷内", 0);
        var msg1 = ArchiveXls.check(archiveXlsList);
        if (!msg1.isEmpty())
            return Result.bad("卷皮表单有格式错误", msg1);
        var msg2 = ArchiveContentXls.check(contentList.toArray(new ArchiveContentXls[0]));
        if (!msg2.isEmpty())
            return Result.bad("卷内表单有格式错误", msg2);
        return  archiveService.importToTab(archiveXlsList, contentList,documentClassId);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
        util.importTemplateExcel(response, "用户数据");
    }

}